﻿using System;
using System.Data;
using System.Xml.Linq;

namespace YArchitech.MEP
{
	public class HYPipeSpecification : IComparable
	{
		public int DN { get; set; }

		public double DN2 { get; set; }

		public double Diameter { get; set; }

		public double Thickness { get; set; }

		public double SComm { get; set; }

		public double SMin { get; set; }

		public HYRange VRange { get; set; }

		public HYRange LRange { get; set; }

		public HYRange RRange { get; set; }

		public int CompareTo(object obj)
		{
			if (obj == null || !(obj is HYPipeSpecification))
			{
				return 1;
			}
			HYPipeSpecification hypipeSpecification = (HYPipeSpecification)obj;
			return this.DN.CompareTo(hypipeSpecification.DN);
		}

		public static HYPipeSpecification FromDataReader(IDataReader reader)
		{
			return new HYPipeSpecification
			{
				DN = Convert.ToInt32(reader["DN"]),
				Diameter = Convert.ToDouble(reader["D"]),
				Thickness = Convert.ToDouble(reader["T"]),
				SComm = Convert.ToDouble(reader["SComm"]),
				SMin = Convert.ToDouble(reader["SMin"]),
				VRange = HYRange.FromString(Convert.ToString(reader["VR"])),
				LRange = HYRange.FromString(Convert.ToString(reader["LR"])),
				RRange = HYRange.FromString(Convert.ToString(reader["RR"]))
			};
		}

		public void WriteXml(XElement xe)
		{
			xe.Add(new object[]
			{
				new XAttribute("DN", this.DN),
				new XAttribute("D", this.Diameter),
				new XAttribute("T", this.Thickness),
				new XAttribute("SMin", this.SMin),
				new XAttribute("SComm", this.SComm),
				new XAttribute("VR", this.VRange.ToString()),
				new XAttribute("LR", this.LRange.ToString()),
				new XAttribute("RR", this.RRange.ToString())
			});
		}

		public void ReadXml(XElement xe)
		{
			this.DN = (int)xe.Attribute("DN");
			this.Diameter = (double)xe.Attribute("D");
			this.Thickness = (double)xe.Attribute("T");
			this.SMin = (double)xe.Attribute("SMin");
			this.SComm = (double)xe.Attribute("SComm");
			this.VRange = HYRange.FromString(xe.Attribute("VR").Value);
			this.LRange = HYRange.FromString(xe.Attribute("LR").Value);
			this.RRange = HYRange.FromString(xe.Attribute("RR").Value);
		}
	}
}
